//
// Created by yanhai on 2020/2/20.
//

#include <boost/core/ignore_unused.hpp>
#include "server/http_server.hpp"

class Count {
    friend std::ostream &operator<<(std::ostream &out, const Count &count);
public:
    Count()
    {
        count = 0;
    }

    Count &operator++()
    {
        ++count;
        return *this;
    }

    Count operator++(int)
    {
        Count tmp;
        tmp.count = count;
        ++count;
        return tmp;
    }

private:
    int count;
};

std::ostream &operator<<(std::ostream &out, const Count &count)
{
    out << "request count: " << count.count;
    return out;
}

/**
 * test:
 *  curl localhost:8000/v1/test
 *  curl localhost:8000/v1/test -X POST
 * @param request
 * @param response
 * @param arg
 */
static void test(Count &count, const Request *request, Response *response)
{
    boost::ignore_unused(request);
    ++count;
    std::cout << count << std::endl;
    response->write("hello world!");
}

int main()
{
    Count count;
    auto &httpServer = HTTPServer::instance();
    httpServer.set_cb("/v1/test",
                      std::bind(test, count,
                                std::placeholders::_1, std::placeholders::_2),
                      EVHTTP_REQ_GET | EVHTTP_REQ_POST);
    httpServer.listen(8000, "0.0.0.0");
    httpServer.start();
    return 0;
}
